package com.example.hoangoanhwedding;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.androidquery.callback.AjaxStatus;
import com.androidquery.callback.LocationAjaxCallback;

public class MainActivity extends Activity implements OnClickListener {

	private final String HOANG_HOME = "21.086432,105.669787";
	private final String OANH_HOME = "20.884094,105.86518";

	private final boolean HOANG = true;
	private final boolean OANH = false;

	private boolean type;

	private Button btnGoToHoang;
	private Button btnGoToOanh;
	private Button btnCallHoang;
	private Button btnCallOanh;
	private Button btnInfoHoang;
	private Button btnInfoOanh;

	private Location myLocation;

	private int numberRequestMyLocation;
	private Handler handlerRequestMyLocation = new Handler();
	private Runnable runnableRequestMyLocation = new Runnable() {

		@Override
		public void run() {
			if (myLocation == null) {
				if (numberRequestMyLocation < 20) {
					getMyLocation();
					handlerRequestMyLocation.postDelayed(runnableRequestMyLocation, 500);
					numberRequestMyLocation++;
				} else {
					numberRequestMyLocation = 0;
					showNotify();
				}
			} else {
				openNavigation();
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		btnGoToHoang = (Button) findViewById(R.id.btnGoToHoang);
		btnGoToOanh = (Button) findViewById(R.id.btnGoToOanh);
		btnCallHoang = (Button) findViewById(R.id.btnCallHoang);
		btnCallOanh = (Button) findViewById(R.id.btnCallOanh);
		btnInfoHoang = (Button) findViewById(R.id.btnInfoHoang);
		btnInfoOanh = (Button) findViewById(R.id.btnInfoOanh);

		btnGoToHoang.setOnClickListener(this);
		btnGoToOanh.setOnClickListener(this);
		btnCallHoang.setOnClickListener(this);
		btnCallOanh.setOnClickListener(this);
		btnInfoHoang.setOnClickListener(this);
		btnInfoOanh.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v == btnGoToHoang) {
			if (NetworkUtility.getInstance(this).isNetworkAvailable()) {
				type = HOANG;
				handlerRequestMyLocation.post(runnableRequestMyLocation);
			} else {
				showDialogNoNetwork();
			}

		} else if (v == btnGoToOanh) {
			if (NetworkUtility.getInstance(this).isNetworkAvailable()) {
				type = OANH;
				handlerRequestMyLocation.post(runnableRequestMyLocation);
			} else {
				showDialogNoNetwork();
			}
		} else if (v == btnCallHoang) {
			Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:0982191648"));
			startActivity(callIntent);

		} else if (v == btnCallOanh) {
			Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:0906265909"));
			startActivity(callIntent);

		} else if (v == btnInfoHoang) {
			showDialog(R.string.infoHoang, R.string.contentInfoHoang);

		} else if (v == btnInfoOanh) {
			showDialog(R.string.infoOanh, R.string.contentInfoOanh);
		}
	}

	public Location getMyLocation() {
		locationAjax();
		return myLocation;
	}

	private void locationAjax() {
		LocationAjaxCallback cb = new LocationAjaxCallback();
		cb.weakHandler(this, "locationCb").timeout(40 * 1000);
		cb.async(this);
	}

	public void locationCb(String url, Location loc, AjaxStatus status) {
		if (loc != null) {
			myLocation = loc;
		}
	}

	private void showNotify() {
		Toast.makeText(this, getString(R.string.notifyNotFindMylocation), Toast.LENGTH_LONG).show();
	}

	private void openNavigation() {
		if (myLocation != null) {
			Intent navigation;

			if (type == HOANG) {
				navigation = new Intent(Intent.ACTION_VIEW,
						Uri.parse("http://maps.google.com/maps?saddr=" + myLocation.getLatitude()
								+ "," + myLocation.getLongitude() + "&daddr=" + HOANG_HOME
								+ "&dirflg=w"));
			} else {
				navigation = new Intent(Intent.ACTION_VIEW,
						Uri.parse("http://maps.google.com/maps?saddr=" + myLocation.getLatitude()
								+ "," + myLocation.getLongitude() + "&daddr=" + OANH_HOME
								+ "&dirflg=w"));
			}

			startActivity(navigation);
		}
	}

	private void showDialogNoNetwork() {
		Toast.makeText(this, getString(R.string.notifyNoNetwork), Toast.LENGTH_LONG).show();
	}

	private void showDialog(int title, int content) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(title).setMessage(content)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		// Create the AlertDialog object and return it
		builder.show();
	}

}
